package me.shurufa.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.activities.LoginActivity;
import me.shurufa.activities.NewMainActivity;
import me.shurufa.model.LoginResponse;
import me.shurufa.net.API;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.Settings;
import me.shurufa.utils.Utils;

/**
 * 登录Fragment
 *
 * @FileName: me.shurufa.fragments.LoginFragment.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-15 10:02
 */
public class LoginFragment extends AuthenticatorFragment implements View.OnClickListener {
    @Bind(R.id.mobile_edit)
    EditText mobileEdit;
    @Bind(R.id.password_edit)
    EditText passwordEdit;
    @Bind(R.id.log_in_button)
    Button logInButton;
    @Bind(R.id.read_text)
    TextView readText;
    @Bind(R.id.go_register)
    TextView goRegister;
    @Bind(R.id.notice_layout)
    LinearLayout noticeLayout;

    private int fromWhere;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        fromWhere = ((LoginActivity) getActivity()).fromWhere;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        if (Global.DEBUG) {
            mobileEdit.setText("15222591730");
            passwordEdit.setText("yyg1990918");
            Utils.moveCursorToLast(mobileEdit);
        }
        logInButton.setOnClickListener(this);
        readText.setOnClickListener(this);
        goRegister.setOnClickListener(this);
    }

    private boolean isInputValid() {
        return !TextUtils.isEmpty(mobileEdit.getText().toString()) && !TextUtils
                .isEmpty(passwordEdit.getText().toString());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.log_in_button:
                if (isInputValid()) {
                    Map<String, Object> params = new HashMap<>();
                    params.put("mobile", mobileEdit.getText().toString());
                    params.put("password", passwordEdit.getText().toString());
                    params.put("platform", "android"); //  平台android
                    doLogin(params);
                }
                break;
            case R.id.go_register:
            case R.id.read_text:
                EventBus.getDefault().post(new Intent(Constants.ACTION_LOGIN_SWITCH));
                break;
        }
    }

    void doLogin(final Map<String, Object> params) {
        new RequestServerTask<LoginResponse>(LoginResponse.class, this, getString(R.string.log_in_please_wait)) {
            @Override
            protected String requestServer() {
                return HttpUtil.post(API.USER_LOGIN, params);
            }

            @Override
            protected void onSuccess(LoginResponse result) {
                saveUserInfo(result);
                //TODO 发起Intent请求跳转至MainActivity
                //Intent intent = new Intent(getActivity(), MainActivity.class);

                handleStartActivity();

            }
        }
        .setDefaultErrMsg(R.string.log_in_failed)
        .start();
    }

    private void handleStartActivity() {
        if (fromWhere == Constants.TYPE_FROM_MAIN_TO_LOGIN) {
            Intent in = new Intent(Constants.EVENT_SHOW_USER_AVATAR);
            EventBus.getDefault().post(in);
        }
        Intent intent = new Intent(getActivity(), NewMainActivity.class);
        startActivity(intent);
        getActivity().finish();
    }

    @Override
    protected void saveUserInfo(LoginResponse result) {
        super.saveUserInfo(result);
        Settings.setCurrentUid(result.data.uid);
    }

}
